package es

import (
	"context"
	elastic "github.com/elastic/go-elasticsearch/v7"
	"github.com/loveuer/esgo2dump/xes/es7"
	"github.com/loveuer/nf/nft/log"
	"net/url"
	"ultone/internal/opt"
	"ultone/internal/tool"
)

var (
	Client *elastic.Client
)

func New(ctx context.Context, uri string) (*elastic.Client, error) {
	var (
		err    error
		client *elastic.Client
		ins    *url.URL
	)

	if ins, err = url.Parse(uri); err != nil {
		return nil, err
	}

	log.Debug("es.InitClient url parse uri: %s, result: %+v", opt.Cfg.ES.Uri, ins)

	if client, err = es7.NewClient(tool.Timeout(10), ins); err != nil {
		return nil, err
	}

	return client, nil
}

func Init(ctx context.Context, uri string) (err error) {
	Client, err = New(ctx, uri)
	return err
}
